package com.ethereal.petmanagement.controller;

import com.ethereal.petmanagement.dao.impl.PetDaoImpl;
import com.ethereal.petmanagement.dto.PageDTO;
import com.ethereal.petmanagement.dto.PetDTO;
import com.ethereal.petmanagement.dto.ResponseDTO;
import com.ethereal.petmanagement.entity.Pet;
import com.ethereal.petmanagement.entity.User;
import com.ethereal.petmanagement.service.IPetService;
import com.ethereal.petmanagement.service.impl.PetServiceImpl;
import com.ethereal.petmanagement.util.BeanUtil;
import com.ethereal.petmanagement.util.CopyUtil;
import com.ethereal.petmanagement.util.RequestDispatcherUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.modelmapper.ModelMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = {"*.pet"})
public class PetController extends HttpServlet {
    static IPetService petService = new PetServiceImpl();
    @Override //doPost
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //获取请求的URI地址信息
        String uri = req.getRequestURI();
        //截取其中的方法名
        String methodName = uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf("."));
        try {
            //使用反射机制获取在本类中声明了的方法
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            // 允许访问private方法
            method.setAccessible(true);
            // 执行方法
            method.invoke(this, req, resp);
        } catch (Exception e) {
            throw new RuntimeException("调用方法出错！");
        }

    }
    @Override //doGet
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        doPost(req, resp);
    }

    //添加宠物
    private void addPet(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        PetDTO petDTO = BeanUtil.mapRequestToBean(req, PetDTO.class);
        petDTO.setUserId(user.getId());
        ResponseDTO<Boolean> responseDTO = petService.addPet(petDTO);
        if (responseDTO.getCode() != 0){
            RequestDispatcherUtil.dispatcher(req, resp, responseDTO);
        }
        req.setAttribute("msg", "添加成功！");
        resp.sendRedirect("getPetList.pet");
    }

    //获取宠物列表
    private void getPetList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PetDTO petDTO = new PetDTO();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        petDTO.setUserId(user.getId());
        ResponseDTO<PageDTO<PetDTO>> responseDTO = petService.getPetList(petDTO);
        if (responseDTO.getCode() != 0){
            RequestDispatcherUtil.dispatcher(req, resp, responseDTO);
        }
        List<PetDTO> petDTOList = responseDTO.getData().getList();
        req.setAttribute("pet", petDTOList);
        req.getRequestDispatcher("/Pet/myPet.jsp").forward(req, resp);
    }

    //删除宠物
    private void deletePet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PetDTO petDTO = new PetDTO();
        petDTO.setId(new BigInteger(req.getParameter("id")));
        ResponseDTO<Boolean> responseDTO = petService.deletePet(petDTO);
        if (responseDTO.getCode() != 0){
            RequestDispatcherUtil.dispatcher(req, resp, responseDTO);
        }
        resp.sendRedirect("getPetList.pet");
    }

    //修改宠物
    private void updatePet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        PetDTO petDTO = BeanUtil.mapRequestToBean(req, PetDTO.class);
        petDTO.setUserId(user.getId());
        petDTO.setId(new BigInteger(req.getParameter("id")));
        ResponseDTO<Boolean> responseDTO = petService.updatePet(petDTO);
        if (responseDTO.getCode() != 0){
            RequestDispatcherUtil.dispatcher(req, resp, responseDTO);
        }
        req.setAttribute("msg", "修改成功！");
        resp.sendRedirect("getPetList.pet");
    }
}
